let express=require("express")();
let expressStatic=require("express-static");
let mysql=require("mysql");
const port=81;
// 规划一个数据库连接
let sql=mysql.createConnection({
	host:"localhost",
	user:"root",
	password:"123456",
	database:"test"
});
// 尝试连接数据库
sql.connect();
express.get("/getuser",(request,response)=>{
		sql.query(`INSERT INTO user (username,password) VALUES ("${request.query.username}","${request.query.password}")`,(error,data)=>{
				if(error){
					response.send("error");
					console.log(error);
				}
				else{
					response.end("success");
					console.log(`${request.query.username}用户新增成功!`)
				}
		})
	})
	express.get("/login",(request,response)=>{
		// request.query
		sql.query(`SELECT * FROM user WHERE username="${request.query.username}" AND password="${request.query.password}"`,(error,data)=>{
			if(error){
				response.send("error");
				console.log(error);
			}
			else{
				if(data.length === 0){
					response.send("error")
				}
				else{
					response.end("success")
				}
			}
		})
	})
express.get("/",(request,response)=>{
	response.send("success!!!!!")
})
// 如果接收到来自前端的静态文件请求的话,我该去哪里找
express.use(expressStatic(__dirname + `/static`))
console.log(`running at ${port}`)
express.listen(port)